{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# User's Guide, Chapter 46: Streams 3: Manipulation, inPlace, and deepcopy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just getting started here..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Replace all consecutive notes of a Stream with intervals while maintaining the hierarchy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import copy\n",
    "\n",
    "def intervalStream(inputStream):\n",
    "    out = copy.deepcopy(inputStream)\n",
    "    for n in out.recurse().getElementsByClass('Note'):\n",
    "        nNext = n.next('GeneralNote')\n",
    "        if nNext is None or not nNext.isNote:\n",
    "            continue # Rests, Chords, etc.\n",
    "        n.activeSite.replace(n, interval.Interval(n, nNext))\n",
    "    for other in out.recurse().getElementsNotOfClass(['Interval', 'Stream']):\n",
    "        other.activeSite.remove(other)  # get rid of everyhing that is not an Interval or Stream\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0.0} <music21.stream.Part Soprano>\n",
      "    {0.0} <music21.stream.Measure 0 offset=0.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {0.5} <music21.interval.Interval M-2>\n",
      "    {1.0} <music21.stream.Measure 1 offset=1.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "        {1.0} <music21.interval.Interval M2>\n",
      "        {2.0} <music21.interval.Interval m3>\n",
      "        {3.0} <music21.interval.Interval m-3>\n",
      "    {5.0} <music21.stream.Measure 2 offset=5.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {1.0} <music21.interval.Interval M-2>\n",
      "        {2.0} <music21.interval.Interval M3>\n",
      "        {3.0} <music21.interval.Interval M-3>\n",
      "    {9.0} <music21.stream.Measure 3 offset=9.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "        {0.5} <music21.interval.Interval m-3>\n",
      "        {1.0} <music21.interval.Interval M-2>\n",
      "        {2.0} <music21.interval.Interval m3>\n",
      "        {3.0} <music21.interval.Interval M2>\n",
      "    {13.0} <music21.stream.Measure 4 offset=13.0>\n",
      "        {0.0} <music21.interval.Interval P1>\n",
      "        {1.0} <music21.interval.Interval P-4>\n",
      "        {2.0} <music21.interval.Interval M-2>\n",
      "        {3.0} <music21.interval.Interval P4>\n",
      "{0.0} <music21.stream.Part Alto>\n",
      "    {0.0} <music21.stream.Measure 0 offset=0.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "    {1.0} <music21.stream.Measure 1 offset=1.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {1.0} <music21.interval.Interval P1>\n",
      "        {2.0} <music21.interval.Interval P1>\n",
      "        {3.0} <music21.interval.Interval P1>\n",
      "    {5.0} <music21.stream.Measure 2 offset=5.0>\n",
      "        {0.0} <music21.interval.Interval P4>\n",
      "        {0.5} <music21.interval.Interval m-2>\n",
      "        {1.0} <music21.interval.Interval M-3>\n",
      "        {2.0} <music21.interval.Interval M3>\n",
      "        {3.0} <music21.interval.Interval M-2>\n",
      "    {9.0} <music21.stream.Measure 3 offset=9.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "        {0.5} <music21.interval.Interval m-3>\n",
      "        {1.0} <music21.interval.Interval M-3>\n",
      "        {2.0} <music21.interval.Interval P4>\n",
      "        {3.0} <music21.interval.Interval P1>\n",
      "    {13.0} <music21.stream.Measure 4 offset=13.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {1.0} <music21.interval.Interval m-2>\n",
      "        {2.0} <music21.interval.Interval M-2>\n",
      "        {3.0} <music21.interval.Interval P1>\n",
      "{0.0} <music21.stream.Part Tenor>\n",
      "    {0.0} <music21.stream.Measure 0 offset=0.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "        {0.5} <music21.interval.Interval M2>\n",
      "    {1.0} <music21.stream.Measure 1 offset=1.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {1.0} <music21.interval.Interval M-2>\n",
      "        {2.0} <music21.interval.Interval M2>\n",
      "        {3.0} <music21.interval.Interval M-2>\n",
      "    {5.0} <music21.stream.Measure 2 offset=5.0>\n",
      "        {0.0} <music21.interval.Interval P5>\n",
      "        {0.5} <music21.interval.Interval P1>\n",
      "        {1.0} <music21.interval.Interval M-2>\n",
      "        {1.5} <music21.interval.Interval m-2>\n",
      "        {2.0} <music21.interval.Interval P1>\n",
      "        {3.0} <music21.interval.Interval P1>\n",
      "    {9.0} <music21.stream.Measure 3 offset=9.0>\n",
      "        {0.0} <music21.interval.Interval m2>\n",
      "        {0.5} <music21.interval.Interval m-2>\n",
      "        {1.0} <music21.interval.Interval M-2>\n",
      "        {1.5} <music21.interval.Interval M-2>\n",
      "        {2.0} <music21.interval.Interval M3>\n",
      "        {3.0} <music21.interval.Interval M-2>\n",
      "    {13.0} <music21.stream.Measure 4 offset=13.0>\n",
      "        {0.0} <music21.interval.Interval P1>\n",
      "        {1.0} <music21.interval.Interval P1>\n",
      "        {2.0} <music21.interval.Interval M-2>\n",
      "        {2.5} <music21.interval.Interval m-2>\n",
      "        {3.0} <music21.interval.Interval M-2>\n",
      "{0.0} <music21.stream.Part Bass>\n",
      "    {0.0} <music21.stream.Measure 0 offset=0.0>\n",
      "        {0.0} <music21.interval.Interval m-2>\n",
      "        {0.5} <music21.interval.Interval M-2>\n",
      "    {1.0} <music21.stream.Measure 1 offset=1.0>\n",
      "        {0.0} <music21.interval.Interval M2>\n",
      "        {1.0} <music21.interval.Interval m2>\n",
      "        {2.0} <music21.interval.Interval m-2>\n",
      "        {3.0} <music21.interval.Interval m2>\n",
      "    {5.0} <music21.stream.Measure 2 offset=5.0>\n",
      "        {0.0} <music21.interval.Interval m-6>\n",
      "        {0.5} <music21.interval.Interval m3>\n",
      "        {1.0} <music21.interval.Interval P-5>\n",
      "        {2.0} <music21.interval.Interval A5>\n",
      "        {3.0} <music21.interval.Interval m2>\n",
      "    {9.0} <music21.stream.Measure 3 offset=9.0>\n",
      "        {0.0} <music21.interval.Interval P-5>\n",
      "        {0.5} <music21.interval.Interval M2>\n",
      "        {1.0} <music21.interval.Interval P-5>\n",
      "        {2.0} <music21.interval.Interval P8>\n",
      "        {3.0} <music21.interval.Interval M2>\n",
      "    {13.0} <music21.stream.Measure 4 offset=13.0>\n",
      "        {0.0} <music21.interval.Interval M-2>\n",
      "        {0.5} <music21.interval.Interval M2>\n",
      "        {1.0} <music21.interval.Interval m2>\n",
      "        {1.5} <music21.interval.Interval M2>\n",
      "        {2.0} <music21.interval.Interval P-8>\n",
      "        {2.5} <music21.interval.Interval M2>\n",
      "        {3.0} <music21.interval.Interval P4>\n"
     ]
    }
   ],
   "source": [
    "b = corpus.parse('bwv66.6')\n",
    "ints = intervalStream(b.measures(0, 4))\n",
    "ints.show('text')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
